﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Common;
using WebSite.Core;
using System.Configuration;

namespace WebSite.Server
{
    /// <summary>
    /// VoteTeam 的摘要说明
    /// </summary>
    public class VoteTeam : IHttpHandler, System.Web.SessionState.IRequiresSessionState
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/json";

            RetInfo response = RetInfoHelper.WebServiceReturn(delegate()
            {
                throw new RuntimeException(20, "对不起，投票已经结束");

                int teamid = StringHelper.Convert(context.Request["teamid"], 0);

                if (Project_Session.CurrentUser == null)
                {
                    throw new RuntimeException(2, "请先登录");
                }

                if (teamid == 0)
                {
                    throw new RuntimeException(31, "无效的队伍");
                }

                /*
                string filter = ConfigurationManager.AppSettings["VoteTeamFilter"] ?? string.Empty;
                var fdummy = filter.Split( ',' );
                List<int> listFilter = new List<int>( Array.ConvertAll<string, int>( fdummy, s => int.Parse( s ) ) );

                if (listFilter.Contains( teamid ))
                {
                    throw new RuntimeException( 99, "他们是线下活动晋级宝宝帮，不参与投票了喔" );
                }*/

                Maticsoft.BLL.CarrefourCover.Team teambll = new Maticsoft.BLL.CarrefourCover.Team();
                var teammodel = teambll.GetModel(teamid);
                if (teammodel == null || teammodel.Type<2)
                {
                    throw new RuntimeException(31, "无效的队伍");
                }

                Maticsoft.BLL.CarrefourCover.Vote votebll = new Maticsoft.BLL.CarrefourCover.Vote();

                // 每日投票限制
                var todaycount = votebll.GetTodayVoteCount(Project_Session.CurrentUserID);
                if (todaycount >= 22)
                {
                    throw new RuntimeException(33, "您今日的投票机会已经用完");
                }

                // 单日只能对一个组队投1次票
                var todayteamcount = votebll.GetTodayVoteTargetCount(Project_Session.CurrentUserID, teamid);
                if (todayteamcount >= 1)
                {
                    throw new RuntimeException(32, "您今天已经给他们投过票了");
                }

                // 添加投票记录
                votebll.Add(new Maticsoft.Model.CarrefourCover.Vote()
                {
                    Ip = context.Request.UserHostAddress,
                    CreateTime = DateTime.Now,
                    AccountId = Project_Session.CurrentUserID,
                    VoteTargetType = 0,
                    VoteTargetId = teamid
                });

                // 更新计数器
                if (teammodel.TeamVoteNum.HasValue)
                {
                    teammodel.TeamVoteNum = teammodel.TeamVoteNum.Value + 1;
                }
                else
                {
                    teammodel.TeamVoteNum = 1;
                }
                teambll.Update(teammodel);

                // 返回计数
                return teammodel.TeamVoteNum;
            });

            context.Response.Write(response.SerializeToString(Common.Serialization.SerializationType.Json));
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}